home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / rpclib / getrpcent.c < prev    next >
C/C++ Source or Header  |  1994-03-09  |  6KB  |  266 lines

  1. /*
  2.  * $Id: getrpcent.c,v 1.3 1994/02/25 00:21:41 jraja Exp $
  3.  *
  4.  * $Log: getrpcent.c,v $
  5.  * Revision 1.3  1994/02/25  00:21:41  jraja
  6.  * Changed malloc, calloc and free to mem_alloc, mem_calloc and mem_free,
  7.  * respectively.
  8.  *
  9.  * Revision 1.2  1993/11/10  02:04:34  jraja
  10.  * ANSI Prototypes, fixed includes and various types.
  11.  * Made *ent() functions static.
  12.  * Changed the rpc database path for the AMITCP.
  13.  *
  14.  */
  15. /* @(#)getrpcent.c    2.2 88/07/29 4.0 RPCSRC */
  16. #if !defined(lint) && defined(SCCSIDS)
  17. static  char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11  Copyr 1984 Sun Micro";
  18. #endif
  19.  
  20. /*
  21.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  22.  * unrestricted use provided that this legend is included on all tape
  23.  * media and as a part of the software program in whole or part.  Users
  24.  * may copy or modify Sun RPC without charge, but are not authorized
  25.  * to license or distribute it to anyone else except as part of a product or
  26.  * program developed by the user.
  27.  * 
  28.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  29.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  30.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  31.  * 
  32.  * Sun RPC is provided with no support and without any obligation on the
  33.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  34.  * modification or enhancement.
  35.  * 
  36.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  37.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  38.  * OR ANY PART THEREOF.
  39.  * 
  40.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  41.  * or profits or other special, indirect and consequential damages, even if
  42.  * Sun has been advised of the possibility of such damages.
  43.  * 
  44.  * Sun Microsystems, Inc.
  45.  * 2550 Garcia Avenue
  46.  * Mountain View, California  94043
  47.  */
  48.  
  49. /*
  50.  * Copyright (c) 1985 by Sun Microsystems, Inc.
  51.  */
  52.  
  53. #include <sys/param.h>
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <sys/types.h>
  57. #include <rpc/rpc.h>
  58. #include <netdb.h>
  59. #include <sys/socket.h>
  60.  
  61. /*
  62.  * Internet version.
  63.  */
  64. struct rpcdata {
  65.     FILE    *rpcf;
  66. #ifndef AMIGA
  67.     char    *current;
  68.     int    currentlen;
  69. #endif
  70.     int    stayopen;
  71. #define    MAXALIASES    35
  72.     char    *rpc_aliases[MAXALIASES];
  73.     struct    rpcent rpc;
  74.     char    line[BUFSIZ+1];
  75.     char    *domain;
  76. } *rpcdata;
  77.  
  78. static struct rpcdata *_rpcdata(void);
  79.  
  80. /*
  81.  * The ent-functions have been made static, since the database
  82.  * implementation should be hidden from the applications (J.R.)
  83.  */
  84. static void setrpcent(int f);
  85. static void endrpcent(void);
  86. static struct rpcent * getrpcent(void);
  87. static struct rpcent * interpret(char *val, size_t len);
  88.  
  89. #ifndef AMIGA
  90. static    char *index();
  91. #endif
  92.  
  93. #if defined(AMITCP)
  94. static char RPCDB[] = "AmiTCP:db/rpc";
  95. #else
  96. static char RPCDB[] = "/etc/rpc";
  97. #endif
  98.  
  99. static struct rpcdata *
  100. _rpcdata(void)
  101. {
  102.     register struct rpcdata *d = rpcdata;
  103.  
  104.     if (d == 0) {
  105.         d = (struct rpcdata *)mem_calloc(1, sizeof (struct rpcdata));
  106.         rpcdata = d;
  107.     }
  108.     return (d);
  109. }
  110.  
  111. struct rpcent *
  112. getrpcbynumber(number)
  113.     register int number;
  114. {
  115.     register struct rpcdata *d = _rpcdata();
  116.     register struct rpcent *p;
  117.  
  118.     if (d == 0)
  119.         return (0);
  120.     setrpcent(0);
  121.     while (p = getrpcent()) {
  122.         if (p->r_number == number)
  123.             break;
  124.     }
  125.     endrpcent();
  126.     return (p);
  127. }
  128.  
  129. struct rpcent *
  130. getrpcbyname(name)
  131.     char *name;
  132. {
  133.     struct rpcent *rpc;
  134.     char **rp;
  135.  
  136.     setrpcent(0);
  137.     while(rpc = getrpcent()) {
  138.         if (strcmp(rpc->r_name, name) == 0)
  139.             return (rpc);
  140.         for (rp = rpc->r_aliases; *rp != NULL; rp++) {
  141.             if (strcmp(*rp, name) == 0)
  142.                 return (rpc);
  143.         }
  144.     }
  145.     endrpcent();
  146.     return (NULL);
  147. }
  148.  
  149. static void
  150. setrpcent(int f)
  151. {
  152.     register struct rpcdata *d = _rpcdata();
  153.  
  154.     if (d == 0)
  155.         return;
  156.     if (d->rpcf == NULL)
  157.         d->rpcf = fopen(RPCDB, "r");
  158.     else
  159.         rewind(d->rpcf);
  160. #ifndef AMIGA
  161.     if (d->current)
  162.         mem_free(d->current, d->currentlen);
  163.     d->current = NULL;
  164. #endif
  165.     d->stayopen |= f;
  166. }
  167.  
  168. static void
  169. endrpcent(void)
  170. {
  171.     register struct rpcdata *d = _rpcdata();
  172.  
  173.     if (d == 0)
  174.         return;
  175. #ifndef AMIGA
  176.     if (d->current && !d->stayopen) {
  177.         mem_free(d->current, d->currentlen);
  178.         d->current = NULL;
  179.     }
  180. #endif
  181.     if (d->rpcf && !d->stayopen) {
  182.         fclose(d->rpcf);
  183.         d->rpcf = NULL;
  184.     }
  185. }
  186.  
  187. static struct rpcent *
  188. getrpcent(void)
  189. {
  190.     register struct rpcdata *d = _rpcdata();
  191.  
  192.     if (d == 0)
  193.         return(NULL);
  194.     if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
  195.         return (NULL);
  196.     if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
  197.         return (NULL);
  198.     return interpret(d->line, strlen(d->line));
  199. }
  200.  
  201. static struct rpcent *
  202. interpret(char *val, size_t len)
  203. {
  204.     register struct rpcdata *d = _rpcdata();
  205.     char *p;
  206.     register char *cp, **q;
  207.  
  208.     if (d == 0)
  209.         return NULL;
  210.     strncpy(d->line, val, len);
  211.     p = d->line;
  212.     d->line[len] = '\n';
  213.     if (*p == '#')
  214.         return (getrpcent());
  215.     cp = index(p, '#');
  216.     if (cp == NULL)
  217.     {
  218.         cp = index(p, '\n');
  219.         if (cp == NULL)
  220.             return (getrpcent());
  221.     }
  222.     *cp = '\0';
  223.     cp = index(p, ' ');
  224.     if (cp == NULL)
  225.     {
  226.         cp = index(p, '\t');
  227.         if (cp == NULL)
  228.             return (getrpcent());
  229.     }
  230.     *cp++ = '\0';
  231.     /* THIS STUFF IS INTERNET SPECIFIC */
  232.     d->rpc.r_name = d->line;
  233.     while (*cp == ' ' || *cp == '\t')
  234.         cp++;
  235.     d->rpc.r_number = atoi(cp);
  236.     q = d->rpc.r_aliases = d->rpc_aliases;
  237.     cp = index(p, ' ');
  238.     if (cp != NULL)
  239.         *cp++ = '\0';
  240.     else
  241.     {
  242.         cp = index(p, '\t');
  243.         if (cp != NULL)
  244.             *cp++ = '\0';
  245.     }
  246.     while (cp && *cp) {
  247.         if (*cp == ' ' || *cp == '\t') {
  248.             cp++;
  249.             continue;
  250.         }
  251.         if (q < &(d->rpc_aliases[MAXALIASES - 1]))
  252.             *q++ = cp;
  253.         cp = index(p, ' ');
  254.         if (cp != NULL)
  255.             *cp++ = '\0';
  256.         else
  257.         {
  258.             cp = index(p, '\t');
  259.             if (cp != NULL)
  260.                 *cp++ = '\0';
  261.         }
  262.     }
  263.     *q = NULL;
  264.     return (&d->rpc);
  265. }
  266.